

//
//  ChangePasswordViewController.m
//  IM_LearningGYDF
//
//  Created by mac on 16/10/19.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ChangePasswordViewController.h"
#import "BasicModel.h"
#define CHANGE_INSET 10
#define TEXT_HEIGHT 40
#define BUTTON_HEIGHT 40
#define  UPDATE_PASS  @"api/Account/ChangePassword"
#define updateUserInfo @"updateUserInfo"
@interface ChangePasswordViewController ()<UITextFieldDelegate>
{
    UITextField * _OriginalTextField;
    UITextField * _newTextField;
    UITextField * _againTextField;
}
@end

@implementation ChangePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //请输入原始密码
    [self setNavigationTitle:@"修改密码"];
    [self setRightBackNavigation];
    [self setLeftBackNavigation];
    [self creatView];
}

- (void)creatView{
    //旧密码
    UILabel *oldPassWord=[MyControl creatLabelWithFrame:CGRectMake(8, 16, 110, TEXT_HEIGHT) text:@"旧密码："];
    [self.view addSubview:oldPassWord];
    
    
    _OriginalTextField = [MyControl creatTextFieldWithFrame:CGRectMake(oldPassWord.x+oldPassWord.width+8, 16, IPHONE_WIDTH-(oldPassWord.x+oldPassWord.width+8)-8, TEXT_HEIGHT) placeHolder:@"" delegate:self tag:100];
    _OriginalTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _OriginalTextField.secureTextEntry = YES;
    [self.view addSubview:_OriginalTextField];
    
    //新密码
    UILabel *newPassWord=[MyControl creatLabelWithFrame:CGRectMake(8, [_OriginalTextField getView_Y]+CHANGE_INSET+TEXT_HEIGHT, 110, TEXT_HEIGHT) text:@"新密码："];
    [self.view addSubview:newPassWord];
    
    _newTextField = [MyControl creatTextFieldWithFrame:CGRectMake(_OriginalTextField.x, [_OriginalTextField getView_Y]+CHANGE_INSET+TEXT_HEIGHT, _OriginalTextField.width, TEXT_HEIGHT) placeHolder:@"" delegate:self tag:100+1];
    _newTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _newTextField.secureTextEntry = YES;
    [self.view addSubview:_newTextField];
    
    //确认密码
    UILabel *againPassWord=[MyControl creatLabelWithFrame:CGRectMake(8, [_newTextField getView_Y]+CHANGE_INSET+TEXT_HEIGHT, 110, TEXT_HEIGHT) text:@"请确认新密码："];
    [self.view addSubview:againPassWord];
    //确认密码
    _againTextField = [MyControl creatTextFieldWithFrame:CGRectMake(_OriginalTextField.x, [_newTextField getView_Y]+CHANGE_INSET+TEXT_HEIGHT, _OriginalTextField.width, TEXT_HEIGHT) placeHolder:@"" delegate:self tag:100+2];
    _againTextField.secureTextEntry = YES;
    _againTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:_againTextField];
    
    
    UILabel *msgLabel=[MyControl creatLabelWithFrame:CGRectMake(8, _againTextField.y+_againTextField.height+8, IPHONE_WIDTH-16, 40) text:@"提示:为了保证你的安全使用，请输入密码保持在6-20分钟字符之间，尽量使用数学加字母的形式进行填写。"];
    msgLabel.font=FONT_14;
    CGFloat height=[LZXHelper textHeightFromTextString:msgLabel.text width:msgLabel.width fontSize:14];
    msgLabel.textColor=[UIColor lightGrayColor];
    msgLabel.height=height+5;
    [self.view addSubview:msgLabel];
    
    //button 按钮
    UIButton * sureButton = [MyControl creatButtonWithFrame:CGRectMake(8, msgLabel.y+msgLabel.height+20, IPHONE_WIDTH-16, BUTTON_HEIGHT) target:self sel:@selector(changeAccountPassword:) tag:1000 image:nil title:@"确认"];
    [sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    sureButton.layer.cornerRadius = 5;
    //    [sureButton setBackgroundColor:[UIColor blueColor]];
    [sureButton setBackgroundColor:COLOR_MIDBLUE];
    [self.view addSubview:sureButton];
}


-(void)changeAccountPassword:(id)sender{
    if (_OriginalTextField.text.length==0) {
        [LZXHelper postMessageWithText:@"输入密码不能为空!" View:self.view];
        return;
    }else if (_newTextField.text.length==0) {
        [LZXHelper postMessageWithText:@"输入密码不能为空!" View:self.view];
        return;
    }else if (_OriginalTextField.text.length==0) {
        [LZXHelper postMessageWithText:@"输入密码不能为空!" View:self.view];
        return;
    }
    if (![_newTextField.text isEqualToString:_againTextField.text]) {
        [LZXHelper postMessageWithText:@"两次输入的密码不一致!" View:self.view];
        return;
    }
    NSString *cancelButtonTitle = NSLocalizedString(@"否", nil);
    NSString *otherButtonTitle = NSLocalizedString(@"是", nil);
    //        NSString *otherButtonTitle2 = NSLocalizedString(@"拍照", nil); 不支持拍照
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"确认要修改密码吗？" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    // Create the actions.
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action)
                                   {
                                       //cancel Button
                                   }];
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
                                  {
                                      [LZXHelper postLoadViewWithText:HUD_PLEASEWAIT View:self.view];
                                       [self updateUserInfoRequest];
                                  }];
    
    
    // Add the actions.
    [alertController addAction:otherAction];
    //[alertController addAction:otherAction2];  不支持拍照
    [alertController addAction:cancelAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

#pragma mark - 更新用户信息
- (void)updateUserInfoRequest{

    NSDictionary *dict=@{
                         @"oldPassword":_OriginalTextField.text,
                         @"newPassword":_newTextField.text,
                         @"newPhoneNumber":@"",
                         @"newMailBox":@"",
                         @"operateType":@"0",
                         @"newEmpPic":@""
                         //                        @"reqStr":@"weaqtfaw"
                         };
    
    //   字典转json
    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
    NSString* str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    //拼接soap字符串
    NSString* soap = [NSString stringWithFormat:@"<sessionId>%@</sessionId>\n<content>%@</content>",[LZXHelper getSessionID],str];
    [HttpRequest SOAPData:[LZXHelper getServerURL] SoapMthod:updateUserInfo soapBody:soap success:^(id responseObject) {
        [LZXHelper hideLoadViewWithView:self.view];
        
        //解析数据
        BasicModel *model=[[BasicModel alloc]initWithResponse:responseObject];
        if ([model.code isEqualToString:@"200"]) {
            [LZXHelper postMessageWithText:model.msg View:self.view];
            [self.navigationController.viewControllers.firstObject dismissViewControllerAnimated:YES completion:nil];
        }else if([model.code isEqualToString:@"220"]){
            [LZXHelper postMessageWithText:model.msg View:self.view];
        }
    } failure:^(NSError *error) {
        [LZXHelper hideLoadViewWithView:self.view];
    }];
}
//结束编辑
#pragma mark ---UITextFieldDelegate
-(void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.tag == 102) {
        if(![textField.text isEqualToString:_newTextField.text]){
            //[LZXHelper postMessageWithText:@"两次输入的密码不一致!" View:self.view];
            //POST_MESSAGE(@"两次输入的密码不一致!");
//            textField.placeholder = @"                          两次输入的密码不一致";
//            [textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
//            [textField placeholderRectForBounds:CGRectMake(IPHONE_WIGHT-140, 0, 140, TEXT_HEIGHT)];
        }
    
    }
    [textField resignFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    if (textField.tag == 102) {
        NSLog(@"haha 判断两次的密码是否一致");
    }
    return YES;
}
-(BOOL)textFieldShouldClear:(UITextField *)textField{
    return YES;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
